package com.lei.chinese_herbal_medicine.Service.Impl;

import com.lei.chinese_herbal_medicine.Entity.Herb;
import com.lei.chinese_herbal_medicine.Entity.PageData;
import com.lei.chinese_herbal_medicine.Entity.TestDataset;
import com.lei.chinese_herbal_medicine.Entity.TrainDataset;
import com.lei.chinese_herbal_medicine.Mapper.HerbMapper;
import com.lei.chinese_herbal_medicine.Mapper.HerbTypeMapper;
import com.lei.chinese_herbal_medicine.Mapper.TestDatasetMapper;
import com.lei.chinese_herbal_medicine.Mapper.TrainDatasetMapper;
import com.lei.chinese_herbal_medicine.Service.HerbService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * (Herb)表服务实现类
 *
 * @author makejava
 * @since 2024-01-18 10:06:42
 */
@Service("herbService")
public class HerbServiceImpl implements HerbService {
    @Resource
    private HerbMapper herbMapper;
    @Resource
    private TrainDatasetMapper trainDatasetMapper;
    @Resource
    private TestDatasetMapper testDatasetMapper;
    @Override
    public boolean insertHerb(Herb herb) {
        return herbMapper.insert(herb)==1;
    }

    @Override
    public List<Herb> selectHerbsByHerbAndPageData(Herb herb, PageData pageData) {
        List<Herb> list=herbMapper.selectHerbsByHerbAndPageData(herb,pageData);
        pageData.setCount(herbMapper.selectCount());
        return list;
    }

    @Override
    @Transactional
    public boolean deleteHerbById(String id) {
        boolean flag=herbMapper.deleteByPrimaryKey(id)==1;
        if(flag){
            List<TrainDataset> trainDatasets=trainDatasetMapper.selectTrainDatasetByHerbIdWithBLOBs(id,null);
            for (TrainDataset trainDataset : trainDatasets) {
                if(!(trainDatasetMapper.deleteByPrimaryKey(trainDataset.getId())==1)){
                    throw new RuntimeException("训练集删除失败！");
                }
            }
            List<TestDataset> testDatasets=testDatasetMapper.selectTestDatasetByHerbIdWithBLOBs(id,null);
            for (TestDataset testDataset : testDatasets) {
                if(!(testDatasetMapper.deleteByPrimaryKey(testDataset.getId())==1)){
                    throw new RuntimeException("测试集删除失败！");
                }
            }
        }
        return flag;
    }

    @Override
    public boolean updateByPrimaryKeyWithBLOBs(Herb herb) {
        return herbMapper.updateByPrimaryKeyWithBLOBs(herb)==1;
    }

    @Override
    public Herb selectByPrimaryKey(String id) {
        return herbMapper.selectByPrimaryKey(id);
    }
}


